package util.delicious;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

import util.io.FileInput;
import util.webSearch.GoogleQuery;
import util.webSearch.YahooQuery;

public class Evaluation {

	public static void genSuggestions(String file, boolean replaceQuery)
			throws UnsupportedEncodingException {

		FileInput in = new FileInput(file);
		 String AGENT_LIST ="/home/sergio/projects/dictionary/user_agent_list.txt";
		String query = in.readString();

		while (query != null) {

			// google suggestions
			LinkedList<String> set = GoogleQuery.getRelatedSearches(query, AGENT_LIST, 100);

			
			if(!replaceQuery){
				System.out.println(query + "\t"+ "google\t"
						+ getSuggestionsString(set, false, null));

				// google suggestions + for kids
				set = GoogleQuery.getRelatedSearches(query + " for kids", AGENT_LIST, 10);

				System.out.println(query + " for kids\t"+"google for kids\t"
						+ getSuggestionsString(set, false, null));
				// yahoo related suggestions
				set = YahooQuery.getRelatedSearches(query);
				System.out.println(query + "\t"+"yahoo\t"
						+ getSuggestionsString(set, false, null));
				// yahoo related suggestions + for kids
				set = YahooQuery.getRelatedSearches(query + " for kids");
				System.out.println(query + " for kids\t"+"yahoo for kids\t"
						+ getSuggestionsString(set, false, null));
			}else{
				System.out.println(query + "\t"+"google\t"
						+ getSuggestionsString(set, false, null));

				// google suggestions + for kids
				set = GoogleQuery.getRelatedSearches(query + " for kids", AGENT_LIST, 10);

				System.out.println(query + " for kids\t"+"google for kids\t"
						+ getSuggestionsString(set, true, query));
				// yahoo related suggestions
				set = YahooQuery.getRelatedSearches(query);
				System.out.println(query + "\t"+"yahoo\t"
						+ getSuggestionsString(set, true, query));
				// yahoo related suggestions + for kids
				set = YahooQuery.getRelatedSearches(query + " for kids");
				System.out.println(query + " for kids\t"+"yahoo for kids\t"
						+ getSuggestionsString(set, true, query));
				
			}
			

			query = in.readString();
		}

	}

	public static String getSuggestionsString(HashSet<String> set, boolean replaceQuery, String query) {
		String temp = "";
		Iterator<String> iter = set.iterator();
		

		while (iter.hasNext()) {

			String t = iter.next();
			
			
			if(replaceQuery)
				t = t.replace(query, "");
			
			temp = temp +t  + "\t";

		}

		return temp;

	}
	
	public static String getSuggestionsString(LinkedList<String> set, boolean replaceQuery, String query) {
		String temp = "";
		Iterator<String> iter = set.iterator();
		

		while (iter.hasNext()) {

			String t = iter.next();
			
			
			if(replaceQuery)
				t = t.replace(query, "");
			
			temp = temp +t  + "\t";

		}

		return temp;

	}
	
	
	public static void main (String args[]) throws UnsupportedEncodingException{
		
		
		String file="/media/sata_/data/delicious/experiments/popular_queries.txt";
		genSuggestions(file,false);
		
	}

}
